package com.tgy.algorithm.base._贪心;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * 正数数组costs、正数数组profits、正数K、正数M
 * costs[i]表示i号项目的花费
 * profits[i]表示i号项目在扣除花费之后还能挣到的钱(利润)
 * K表示你只能串行的最多做k个项目
 * M表示你初始的资金
 * 说明:每做完一个项目，马上获得的收益，可以支持你去做下一个项目。不能并行的做项目输出:你最后获得的最大钱数。
 */

class Project {
    int cost;
    int profit;
    Project(int cost, int profit) {
        this.cost = cost;
        this.profit = profit;
    }
}

public class _花费利润最大化 {


    public static int getMaxProfits(int[] costs, int[] profits, int cost, int count) {

        int itemLen = costs.length;
        Project[] projects = new Project[itemLen];
        for (int i = 0; i < itemLen; i++) {
            projects[i] = new Project(costs[i], profits[i]);
        }

        PriorityQueue<Project> minHeap = new PriorityQueue<>(new Comparator<Project>() {
            @Override
            public int compare(Project o1, Project o2) {
                if (o1.cost == o2.cost) {
                    return o2.profit - o1.profit;
                }
                return o1.cost - o2.cost;
            }
        });

        PriorityQueue<Project> maxHeap = new PriorityQueue<>(new Comparator<Project>() {
            @Override
            public int compare(Project o1, Project o2) {
                if (o1.profit == o2.profit) {
                    return o2.cost - o1.cost;
                }

                return o2.profit - o1.profit;
            }
        });

        for (int i = 0; i < itemLen; i++) {
            minHeap.add(projects[i]);
        }

        while (count > 0) {
            while (!minHeap.isEmpty()) {
                Project pro = minHeap.peek();
                if (cost >= pro.cost) {
                    minHeap.poll();
                    maxHeap.add(pro);
                }else {
                    break;
                }
            }
            if (maxHeap.isEmpty()) {
                break;
            }
            Project selectPro = maxHeap.poll();
            cost +=  selectPro.profit;
            count--;
        }

        return cost;
    }

    public static void main(String[] args) {
        // 花费 1  1  2  3
        // 利润 1  2  1  2
        // 本钱:1 项目: 2
        int[] costs = new int[]{1,1,2,3};
        int[] profits = new int[]{1,2,1,2};
        int cost = getMaxProfits(costs, profits, 1, 2);
        System.out.println(cost);
    }
}
